package xyz.iguilty.documents.doc.model;

import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.HyperlinkTextRenderData;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.plugin.table.LoopColumnTableRenderPolicy;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.deepoove.poi.policy.RenderPolicy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ObjectUtils;
import xyz.iguilty.documents.common.constant.DocumentConstant;
import xyz.iguilty.documents.common.enums.PicturePosition;
import xyz.iguilty.documents.common.enums.TableEnum;
import xyz.iguilty.documents.common.model.DocumentModel;

import java.io.IOException;
import java.util.Collection;

public class WordModel extends DocumentModel {
    private static LoopRowTableRenderPolicy loopRowTableRenderPolicy = null;
    private static LoopColumnTableRenderPolicy loopColumnTableRenderPolicy = null;

    public void addColorfulText(String key, String text, String color){
        super.addAttribute(key,new TextRenderData(color,text));
    }

    public void addStyleText(String key, String text, Style style){
        super.addAttribute(key,new TextRenderData(text,style));
    }

    public void addHyperlinkText(String key,String text,String link){
        super.addAttribute(key,new HyperlinkTextRenderData(text,link));
    }

    public void addPicture(String key,PictureRenderData picture){
        super.addAttribute(key,picture);
    }

    public void addPicture(String key,String url){
        this.addPicture(key,url,PicturePosition.LOCAL,null,null);
    }

    public void addPicture(String key,String url,PicturePosition position){
        this.addPicture(key,url,position,null,null);
    }

    public void addPicture(String key, String url,Integer width,Integer height){
        this.addPicture(key,url,PicturePosition.LOCAL,width,height);
    }

    public void addPicture(String key, String url,PicturePosition position,Integer width,Integer height){
        if (ObjectUtils.isEmpty(url)){
            super.addAttribute(key,null);
            return;
        }
        Pictures.PictureBuilder pictureBuilder = null;
        if (PicturePosition.CLASSPATH.equals(position)){
            try {
                pictureBuilder = Pictures.ofStream(new ClassPathResource(url).getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
                //fixme 可选张显示不存在的图片
                super.addAttribute(key,null);
                return;
            }
        }else if (PicturePosition.NET.equals(position)||url.startsWith("http://")||url.startsWith("https://")){
            pictureBuilder = Pictures.ofUrl(url);
        }else {
            pictureBuilder = Pictures.ofLocal(url);
        }
        if (width!=null&&height!=null){
            pictureBuilder.size(width,height);
        }
        addPicture(key,pictureBuilder.create());
    }


    public void addTable (String key, Collection<?> value){
        this.addTable(key,value,TableEnum.ROW);
    }

    public void addTable (String key, Collection<?> value, TableEnum tableEnum){
        super.addAttribute(key,value);
        if (TableEnum.COLUMN.equals(tableEnum)){
            if (loopColumnTableRenderPolicy == null) {
                loopColumnTableRenderPolicy =new LoopColumnTableRenderPolicy();
            }
            this.bind(key,loopColumnTableRenderPolicy);
        }else {
            if (loopRowTableRenderPolicy == null) {
                loopRowTableRenderPolicy =new LoopRowTableRenderPolicy();
            }
            this.bind(key,loopRowTableRenderPolicy);
        }
    }

    private ConfigureBuilder getConfiguration() {
        ConfigureBuilder config;
        if (!super.containsKey(DocumentConstant.WORD_CONFIG)){
            config = Configure.builder();
            super.addAttribute(DocumentConstant.WORD_CONFIG, config);
        }else {
            config = (ConfigureBuilder)super.getAttribute(DocumentConstant.WORD_CONFIG);
        }
        return config;
    }

    private void bind(String tagName, RenderPolicy policy) {
        this.getConfiguration().bind(tagName,policy);
    }

}
